package cn.itcast.servlet;

import cn.itcast.domain.Student;
import cn.itcast.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        try {
            String number = req.getParameter("number");
            String name = req.getParameter("name");
            int age =Integer.parseInt(req.getParameter("age"));
            Student student = new Student();
            student.setNumber(number);
            student.setName(name);
            student.setAge(age);
            new StudentServiceImpl().addStudent(student);
            resp.sendRedirect(req.getContextPath()+"/FindAllStudentServlet");
        } catch (Exception e) {
           resp.getWriter().write("服务器炸了");
        }
    }
}
